package com.sunlee.common.service.service.impl;


import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sunlee.common.core.entity.PageData;
import com.sunlee.common.service.dao.ThirdProductDao;
import com.sunlee.common.service.dto.ThirdProductDTO;
import com.sunlee.common.service.entity.ThirdProductEntity;
import com.sunlee.common.service.service.ThirdProductCategoryMappingService;
import com.sunlee.common.service.service.ThirdProductService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 第三方资源表
 *
 * @author sunlee
 */
@Service
public class ThirdProductServiceImpl extends CrudServiceImpl<ThirdProductDao, ThirdProductEntity, ThirdProductDTO> implements ThirdProductService {


    @Autowired
    private ThirdProductCategoryMappingService thirdProductCategoryMappingService;

    @Override
    public PageData<ThirdProductDTO> pageOwn(Map<String, Object> params) {
        IPage<ThirdProductEntity> page = baseDao.selectPage(
                getPage(params, null, false),
                getWrapper(params)
        );

        return getPageData(page, currentDtoClass());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteRelByIds(Long[] pIds) {
        // 1、先删除分类的关联关系
        thirdProductCategoryMappingService.deleteByPIds(Arrays.asList(pIds));

        // 2、删除存在的ZIP文件、PPT文件
        List<ThirdProductEntity> thirdProductEntities = listByIds(Arrays.asList(pIds));
        for (ThirdProductEntity productEntity : thirdProductEntities) {
            String zipPath = productEntity.getZipPath();
            String pptPath = productEntity.getPptPath();

            if (StringUtils.isNotBlank(zipPath) && FileUtil.exist(zipPath)) {
                FileUtil.del(zipPath);
            }
            if (StringUtils.isNotBlank(pptPath) && FileUtil.exist(zipPath)) {
                FileUtil.del(pptPath);
            }
        }

        // 3、删除第三方商品表数据
        deleteOwn(pIds);
    }

    @Override
    public LambdaQueryWrapper<ThirdProductEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");
        String source = (String) params.get("source");
        String title = (String) params.get("title");
        String status = (String) params.get("status");
        String statusMsg = (String) params.get("statusMsg");

        LambdaQueryWrapper<ThirdProductEntity> wrapper = new LambdaQueryWrapper<ThirdProductEntity>()
                .like(StringUtils.isNotBlank(title), ThirdProductEntity::getTitle, title)
                .like(StringUtils.isNotBlank(statusMsg), ThirdProductEntity::getStatusMsg, statusMsg)
                .eq(StringUtils.isNotBlank(source), ThirdProductEntity::getSource, source)
                .eq(StringUtils.isNotBlank(status), ThirdProductEntity::getStatus, status)
                .orderByDesc(ThirdProductEntity::getCreateDate);

        if (StringUtils.isNotBlank(id)) {
            wrapper.and(w -> w.eq(ThirdProductEntity::getId, id)
                    .or()
                    .eq(ThirdProductEntity::getThirdResId, id)
                    .or()
                    .eq(ThirdProductEntity::getResId, id));
        }

        return wrapper;
    }
}




